<?php

    class Search_Index extends ActiveRecord {
        
        function Search_Index()
        {
            $conf = array('table'=>DB_PREFIX.'search_index');
            $this->ActiveRecord($conf);
        }
        
        function prepare_content_for_search_index($text)
        {
            include_once VENDOR.'/safehtml/safehtml.php';
            $safe        =& new safehtml();
            // safe html will try to close elements, making it easier to strip out
            // the tags for the index
            $cleanedhtml = $safe->parse($text);
            $text        = strip_tags($cleanedhtml);
            
            // clean out htmlentities
            $text = str_replace('&nbsp;',' ',$text); // don't screw up spacing
            $text = preg_replace('!&[^;\s]+;!','',$text);
            return $text;
        }
        
        function &do_search($query)
        {
            // do fulltext search
            $s_query = sanitize($query);
            $sql = "SELECT 
                      url, title, content, 
                      MATCH (title,content) AGAINST ('$s_query' IN BOOLEAN MODE) as score
                    FROM `{$this->table}`
                    WHERE MATCH(title,content) AGAINST('$s_query' IN BOOLEAN MODE)
                    ORDER BY score DESC";
            $pager = $this->pager($sql);
            $pager->htmlSpecialChars = false;
            $pager->id = 'se';
            return $pager;
        }
        
    }

?>